package com.anchovy.maven.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth/")
public class AuthController {


    // 测试登录，浏览器访问： http://localhost:9001/auth/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public SaResult doLogin(String username, String password) {
        if (StrUtil.isEmpty(username)) {
            username = "zhang";
        }
        if (StrUtil.isEmpty(password)) {
            password = "123456";
        }
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(1);
            return SaResult.ok("登录成功");
        }
        return SaResult.error("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:9001/auth/isLogin
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    // 查询 Token 信息  ---- http://localhost:9001/auth/tokenInfo
    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    // 测试注销  ---- http://localhost:9001/auth/logout
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

    //强制注销某用户 ---- http://localhost:9001/auth/logoutUser
    @RequestMapping("logoutUser")
    public SaResult logoutUser(String id) {
        StpUtil.logout(id);
        return SaResult.ok();
    }

    //强制踢人下线  ---- http://localhost:9001/auth/kickoutUser
    @RequestMapping("kickoutUser")
    public SaResult kickoutUser(String id) {
        StpUtil.logout(id);
        return SaResult.ok();
    }


    // 查询角色列表  ---- http://localhost:9001/auth/getRoleList
    @RequestMapping("getRoleList")
    public SaResult getRoleList() {
        return SaResult.data(StpUtil.getRoleList());
    }

    // 查询权限列表  ---- http://localhost:9001/auth/getPermissionList`
    @RequestMapping("getPermissionList")
    public SaResult getPermissionList() {
        return SaResult.data(StpUtil.getPermissionList());
    }
}
